package com.example.demofdfs.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.util.List;
import java.util.function.BiConsumer;

import com.alibaba.fastjson.JSON;
import com.example.demofdfs.model.TestA;

public class Http {

	static String ip = "localhost";
	static int port = 80;
	public void get() throws UnknownHostException, IOException {
		InetSocketAddress  address = new InetSocketAddress(ip, port);
		
		Socket socket = new Socket();
		socket.connect(address, 30000);
		
		if (socket.isConnected())
			System.out.println("socket 链接...");
		
		OutputStream out = socket.getOutputStream();
		out.write(getHttp());
		out.flush();
		
		StringBuilder sb = new StringBuilder();
		InputStream in = socket.getInputStream();
		String temp = "";
		String line;
		int length = 0;
		int count = 0;
		boolean flag = true;
		byte[] b = new byte[1];
		int len;
		int countEnter = 0;
        while((len = in.read()) > -1) {
        	char c = (char) len;
        	if (ENTER.equals(Character.toString(c))) {
				countEnter++;
			} else {
				countEnter = 0;
			}
//        	System.out.println((temp = temp + c));
//        	count += line.length();
//        	temp = temp + line;
//        	System.out.println(line);
//        	
//        	if (flag) {
//        		if (line.equals("")) {
//        			flag = false;
//        			count = 0;
//        		}
//        		if (line.startsWith("Content-Length:"))
//        			length = Integer.parseInt(line.replace("Content-Length:", "").trim());
//        	} else {
//        		if (count == length)
//        			break;
//        	}
        	
        	
        }
        System.out.println(temp.substring(100));
		in.close();
		out.close();
		socket.close();
		
	}
	
	public final static String ENTER = System.getProperty("line.separator");
	
	private byte[] getHttp() {
		StringBuilder sb = new StringBuilder();
		sb.append("GET / HTTP/1.1").append(ENTER)
		.append("Host: " + ip + ":" + port).append(ENTER)
//		.append("User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36")
//		.append(ENTER)
		.append(ENTER);
		return sb.toString().getBytes();
	}
	
	private static void httpURL() {
		try {
			HttpURLConnection client = (HttpURLConnection) new URL("http://localhost:8080/test/testA").openConnection();
		
//			StringBuilder params = new StringBuilder();
//			params.append("{").append("\"id\":").append("\"100\"").append("}");
			Object object = new TestA(666L);
			client.setRequestMethod("POST");
			client.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
//			client.setRequestProperty("Accept", "application/json");
			client.setDoOutput(true);
			OutputStream out = client.getOutputStream();
			out.write(JSON.toJSONString(object).getBytes());
			out.flush();
			
			client.connect();
			System.out.println(client);
			System.out.println(client.getDate());
			System.out.println(client.getContentType());
			System.out.println(client.getContentLength());
			System.out.println(client.getContentEncoding());
//			System.out.println(client.getContent());
			System.out.println(client.getHeaderFields());
			System.out.println(client.getInputStream());
			
			client.getHeaderFields().forEach(new BiConsumer<String, List<String>>() {

				@Override
				public void accept(String t, List<String> u) {
					StringBuilder sb = new StringBuilder();
					for (int i = 0; i < u.size(); i++) {
						sb.append(u.get(i)).append(", ");
					}
					System.out.println(t + " = " + sb.toString());
				}
			});
			
			InputStream inputStream = client.getInputStream();
			byte[] b = new byte[235];
			int len;
			StringBuilder sb = new StringBuilder();
			while ((len = inputStream.read(b)) > -1) {
				sb.append(new String(b, 0, len, "UTF-8"));
			}
			if (sb.length() > 0) {
				System.out.println(sb.toString());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) throws UnknownHostException, IOException {
//		new Http().get();
		httpURL();
	}
}
